package com.cskaoyan.order.api;

import com.cskaoyan.mall.commons.result.ServiceCallResponse;
import com.cskaoyan.mall.commons.result.ServiceCallResponseUtil;
import com.cskaoyan.order.dal.entitys.ItemIdAndNum;
import com.cskaoyan.order.dal.entitys.Order;
import com.cskaoyan.order.dal.entitys.OrderItem;
import com.cskaoyan.order.dal.persistence.OrderItemMapper;
import com.cskaoyan.order.dal.persistence.OrderMapper;
import com.cskaoyan.order.dal.persistence.StockMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import tk.mybatis.mapper.entity.Example;

import java.util.List;

/**
 * @auther cskaoyan
 * @date 2022/8/24:10:20
 */
@RestController
public class PayApiImpl {

    @Autowired
    OrderMapper orderMapper;

    @Autowired
    OrderItemMapper itemMapper;

    @Autowired
    StockMapper stockMapper;

    // 关于服务调用
    @PutMapping(value = "/rpc/pay/success")
    public void test(@RequestParam("orderId") String orderId) {
//        String data = "data";
//        // 1. 如果我们的服务调用，需要返回数据，在该服务的api工程，定义一个对应的Response指定泛型为返回的数据类型
//        // 2. 如果我们的服务调用，不需要返回数据
//        return new ServiceCallResponseUtil().ok(data);

//        Example example = new Example(Order.class);
//        Example.Criteria criteria = example.createCriteria();
//        criteria.andEqualTo("status",1);

        //修改订单状态
        int i = orderMapper.updateStatus(orderId);

        //通过orderid在Order—item表里面找到商品id以及对应购买的数量
        List<ItemIdAndNum> itemIdAndNums = itemMapper.selectItemIdAndNumByOrderId(orderId);

        //去库存表修改商品库存

        for (ItemIdAndNum itemIdAndNum : itemIdAndNums) {
            stockMapper.updateStockCountByItemId(itemIdAndNum);
        }



    }


}
